package com.heima.interceptor;

import com.heima.model.entity.AddThreadLocalUtil;
import com.heima.model.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class BackendInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = new User();
        user.setId(1);
        user.setUsername("admin");

        AddThreadLocalUtil.setUser(user);



        return true;
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        AddThreadLocalUtil.clear();

        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
